<?php session_start(); /*Added by suwondo*/ ?>
<?php require('connections/con_gl.php'); ?>
<?php
cekAkses($_SESSION[akses],"JS-6");
$currentPage = $_SERVER["PHP_SELF"];
mysql_select_db($database_con_gl, $con_gl);

$bid = $_GET['barang'];
$jumlah = $_GET['jumlah'];
$checkin = $_GET['checkin'];
$checkout = $_GET['checkout'];
$lamaWaktu = $_GET['jmlhwkt'];
$noref = $_GET['noref'];
global $d;

$d = substr($checkin,8,2);
$s = substr($checkin,8,1);
if($s == 0){
	$d = substr($checkin,9,1);
}
//echo $noref;
echo $jumlah;


$area = $_GET['area'];
list($namaarea) = mysql_fetch_row(mysql_query("select area from gl_area where id = '$area'"));
list($paket) = mysql_fetch_row(mysql_query("select barang from gl_barang where id = '$bid'"));
?>
<script type="text/javascript" src="js/jquery-1.4.js"></script>
<script type="text/javascript" src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="css/jquery.autocomplete.css" />
<h1>Data Kendaraan <?php if($area != '') echo "Area ".$namaarea;?></h1>

 <?php if(($bid != '') and ($bid != false)) echo "<h1>$paket</h1>"?>
<table width="100%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable">
	<tr valign="top" bgcolor="#FFFFFF" class="hide">
        <td colspan="11" align="left" style="border-bottom:solid 1px #E2EAC1; border-right:solid 1px #E2EAC1; font-size: medium;">
			<form id="search" name="search" method="post" action="">
			<!-- Edit by suwondo -->
			  Pencarian data : 
	    <label>
	    <input name="cr_txt" type="text" id="cr_txt" placeholder="Find by: no polisi" onclick="this.select(); $(this).autocomplete('components/kendaraan/search.autocomplete.php?barang=<?php echo $bid; ?>&area=<?php echo $area; ?>', { width: 330 });" size="100%" maxlength="100" onkeyup="javascript: $(this).autocomplete('components/kendaraan/search.autocomplete.php?barang=<?php echo $bid; ?>&area=<?php echo $area; ?>', { width: 330 }); $(this).result(function(event, data, formatted){ idx = formatted.split(' - '); window.location.href='index.php?component=kendaraan&task=list&barang=<?php echo $bid; ?>&area=<?php echo $area; ?>&open=window&cr_kel=ALL&cr_txt='+idx[1]; });" <?php echo $_REQUEST['cr_txt'];?> />
				</label>
				<label><input name="Search" type="submit" id="Search" value="Search" /></label>
			</form>
		<?php
		if(isset($_POST['Search'])) {
			echo "<script>window.location=\"index.php?component=kendaraan&task=list&barang=$bid&area=$area&open=window&tahun=".$_POST[tahun]."&cr_type=".$_POST[cr_type]."&cr_kel=ALL&cr_txt=".str_replace(" ","+",$_POST[cr_txt])."\";</script>";
		}
		?>		</td>
	</tr>
	<tr>
		<th width="500"><strong>Tipe Kendaraan</strong></th>
		<th width="219"><strong>Tahun</strong></th>
		<th width="219"><strong>No. Polisi</strong></th>
		<th width="119"><strong>Unit Stok</strong></th>
		<th width="119"><strong>Cabang</strong></th>
		<th width="132" align="center" class="hide"><strong>Aksi</strong></th>
	</tr>
	<?php
	
	if(trim($_REQUEST['cr_txt']) != '')
	{
		$key = trim($_REQUEST['cr_txt']);
		
	}
		
	if($bid == 'false'){
		$str = "select distinct a.id, a.tipekendaraan, b.tahun, b.nopolisi, b.area, b.cabang,CASE b.unitstok
                   WHEN '1' THEN 'Corp'
				   WHEN '2' THEN 'Retail'
				   WHEN '3' THEN 'Tugas Kantor'
				   ELSE 'Ops /UGC.'
      END AS Unit from gl_tipekendaraan as a inner join gl_masterkendaraan as b on (a.id = b.tipekendaraan) inner join gl_jenbar as c on (a.id = c.jenis) where b.area = '$area'";
						
		$str .= "and (b.nopolisi like '%$key%')";
		
		$str .= " order by b.unitstok, b.tahun desc";
	   
		$query = mysql_query($str) or die (mysql_error()); 
		
	}else{
	
		$str = "select distinct a.id, a.tipekendaraan, b.tahun, b.nopolisi, b.area, b.cabang,CASE b.unitstok
                   WHEN '1' THEN 'Corp'
				   WHEN '2' THEN 'Retail'
				   WHEN '3' THEN 'Tugas Kantor'
				   ELSE 'Ops/UGC.'
			END AS Unit from gl_tipekendaraan as a inner join gl_masterkendaraan as b on (a.id = b.tipekendaraan) inner join gl_jenbar as c on (a.id = c.jenis) where c.barang='$bid' AND b.area = '$area'";
			$str .= "and (b.nopolisi like '%$key%')";
			$str .= "and (b.nopolisi like '%$key%')";
		
			//$str .= " and b.area = '$_SESSION[lokasi]' ";
		$str .= " order by b.unitstok, b.tahun desc";
			//print_r($str);
		$query = mysql_query($str) or die (mysql_error()); 
		
		}
		/*
		$query3 = mysql_query("SELECT booking_date,kendaraan FROM gl_book") or die (mysql_error());
		while($hasil = mysql_fetch_array($query3)){
			$booking[] = $hasil['booking_date'];
			$kendaraan[] = $hasil['kendaraan'];
		}*/
		
		$count = 0;
		if($jumlah == 1){
			$newdate = $checkin;
		}else{
			//$jumlah = $jumlah - 1;
			//$newdate = date('Y/m/d', strtotime('+'.$jumlah. 'days', strtotime($checkin)));;  
			$newdate = $checkout;
		}
		echo $newdate;
		$numResults = mysql_num_rows($query);
		if ($numResults > 0) {
		 	while(list($id, $tipe, $tahun, $nopolisi, $area, $cabang,$Unit) = mysql_fetch_row($query)){
				echo "<tr>";
				list($namaarea) = mysql_fetch_row(mysql_query("select area from gl_area where id='$area'"));
				list($namagudang) = mysql_fetch_row(mysql_query("select gudang from gl_gudang where id='$cabang'"));
				
				
				$query3 = mysql_query("SELECT * FROM gl_book WHERE gl_book.kendaraan = '$nopolisi' && gl_book.booking_date BETWEEN '$checkin' AND '$newdate' && gl_book.no_ref != '$noref' ") or die (mysql_error());				
				$numRow = mysql_num_rows($query3);
				if($numRow < 1){
					echo "<td>$tipe</td><td>$tahun</td><td>$nopolisi</td><td>$Unit</td><td>$namagudang</td><td align='center'><input type='button' value='pilih' onclick=\"javascript: window.opener.document.getElementById('kendaraan').value='$nopolisi'; window.opener.document.getElementById('txtkendaraan').value='$nopolisi';"; 
					if($bid !== 'false'){
						echo " window.opener.getHargaKendaraan(); window.opener.getBiayaBBM(); window.opener.getBiayaTol(); window.opener.getBiayaParkir(); window.opener.getBiayaSupir(); window.opener.getBiayaAkomodasi(); window.opener.getBiayaOther(); window.opener.getTotal(); window.opener.getGrandTotal(); ";
					}
					echo "window.close(1000);\" /></td>";
				}else{
					echo "<td>$tipe</td><td>$tahun</td><td>$nopolisi</td><td>$Unit</td><td>$namagudang</td><td align='center'>TIDAK ADA</td>"; 
					
				}
				
				
				echo "</tr>";
			}
		} else {
		  echo "<tr>";
		  if($bid == 'false'){
		  echo "<td><strong>Maaf data yang anda cari tidak tersedia</strong></td>";
		  }
		  else
		  {
			  if(empty($bid))
			  {
			  echo "<td><strong>Mohon pilih paket yang akan digunakan</strong></td>";
			  }
			   else
			  {
			  echo "<td><strong>Mohon cek kembali kesesuaian tipe kendaraan  dipaket sewa</strong></td>";
			  }
			
		  }
		  echo "</tr>";
		}
		
		
	?>
 </table>
